<?php
$loader->requireOnce('ordo/EquipmentType.class.php');
class SurgeryMedicalRecord extends ORDataObject {
	var $surgery_medical_record_id	= '';
	var $medical_record_id			= '';
	var $surgery_equipment_id	 	= '';
	var $quantity					= '';
	var $vi_quality_title			= '';
	var $en_quality_title			= '';

	var $_table 		= 'surgery_medical_record';
	var $_key 			= 'surgery_medical_record_id';
	var $_internalName	= 'SurgeryMedicalRecord';

	function SurgeryMedicalRecord() {
		parent::ORDataObject();
	}

function getMedicines($medical_record_id){
        $fieldName  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        $fieldTitle  =   L10nHelper::changeField(L10nHelper::getLocale(), 'quantity_title');
        $ds = new Datasource_sql();
        $sql = array(
                    'cols'  =>  "se.surgery_equipment_id medicine_id,
                                et.$fieldName type, se.$fieldName name, sm.quantity, sm.$fieldTitle quantity_title",
                    'from'  =>  "$this->_table sm
                                LEFT JOIN surgery_equipment se ON(sm.surgery_equipment_id = se.surgery_equipment_id)
                                LEFT JOIN equipment_type et ON(se.equipment_type_id = et.equipment_type_id)
                                ",
                    'where' =>  " sm.medical_record_id = $medical_record_id
                            AND se.equipment_type_id != ".EquipmentType::EQUIPMENT_ID
                    );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }
    
    function getEquipments($medical_record_id){
        $fieldName  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        $fieldTitle  =   L10nHelper::changeField(L10nHelper::getLocale(), 'quantity_title');
        $ds = new Datasource_sql();
        $sql = array(
                    'cols'  =>  "se.surgery_equipment_id equipment_id,
                                et.$fieldName type, se.$fieldName name, sm.quantity, sm.$fieldTitle quantity_title ",
                    'from'  =>  "$this->_table sm
                                LEFT JOIN surgery_equipment se ON(sm.surgery_equipment_id = se.surgery_equipment_id)
                                LEFT JOIN equipment_type et ON(se.equipment_type_id = et.equipment_type_id)
                                ",
                    'where' =>  " sm.medical_record_id = $medical_record_id AND se.equipment_type_id = ".EquipmentType::EQUIPMENT_ID
                    );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }

    // TODO localization
    // The equipment used in surgery had moved to drug_material
	function addRecord( $medical_record_id, $equipment_id,  $quantity, $quality_title){
		$data = array(
					'medical_record_id'	=> $medical_record_id,
					'surgery_equipment_id'	=> $equipment_id,
					'quantity'		=> $quantity,
					'vi_quality_title'	=> $quality_title
		);
		$this->populateArray($data);
		$this->persist();
	}

	function deleteAllRecord($medical_record_id){
		$sql = "DELETE FROM $this->_table WHERE medical_record_id = $medical_record_id";
		$this->_execute($sql);
	}

	function SumQuantity($startDate, $endDate, $selectInsurance){
        $field =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
		$condition = '';
		if($selectInsurance == 'no'){
			$condition = " AND t.insurance = 'no'";
		}
		if($selectInsurance == 'yes'){
			$condition = " AND t.insurance != 'no'";
		}
		$sql = array(
                'cols'  =>  "smr.surgery_equipment_id, se.$field name,
                    et.$field type, eu.$field unit,  SUM(smr.quantity) num ",
                'from'  =>  "$this->_table smr
                    INNER JOIN medical_record mr ON (smr.medical_record_id = mr.medical_record_id)
                    INNER JOIN ticket t ON (mr.ticket_id = t.ticket_id )
                    LEFT JOIN surgery_equipment se ON (smr.surgery_equipment_id = se.surgery_equipment_id)
                    LEFT JOIN equipment_type et ON (se.equipment_type_id = et.equipment_type_id)
                    LEFT JOIN equipment_unit eu ON (se.equipment_unit_id = eu.equipment_unit_id)
                    ",
                'where' =>  " '$startDate' <= DATE(t.started)
                        AND DATE(t.started) <= '$endDate' $condition",
                'groupby'   =>  "smr.surgery_equipment_id"
                );
                $ds = new Datasource_sql();
                $ds->setup($this->_db, $sql);
                return $ds->toArray();
	}


}
?>